#include "FileSerializer.h"
#include "DefaultFileStream.h"
#include "EncryptFileStream.h"

CFileSerializer::CFileSerializer(bool bEncrypt, bool bWriteMode)
{
	m_bWriteMode = bWriteMode;
	if (bEncrypt)
	{
		pFS = new CEncryptFileStream();
	}
	else
	{
		pFS = new CDefaultFileStream();
	}
}
CFileSerializer::~CFileSerializer()
{
	if (pFS)
	{
		delete pFS;
		pFS = NULL;
	}
}

bool CFileSerializer::Initialize(const std::string & strFileName)
{
	if (m_bWriteMode)
	{
		return _InitializeW(strFileName);
	}
	else
	{
		return _InitializeR(strFileName);
	}
}
bool CFileSerializer::_InitializeR(const std::string & strFileName)
{
	if(!_FileStream()._fopen( strFileName.c_str(),true ))
	{
		return false;
	}
	return true;
}
bool CFileSerializer::_InitializeW(const std::string & strFileName)
{
	if(!_FileStream()._fopen(strFileName.c_str(), false))
	{
		return false;
	}
	return true;
}

void CFileSerializer::Close()
{
	_FileStream()._fclose();
}
IFileStream & CFileSerializer::_FileStream()
{
	return *pFS;
}

void CFileSerializer::Serialize(bool & rBoolData)
{
	if(m_bWriteMode)
	{
		_FileStream()._fwrite(&rBoolData, sizeof(bool), 1);
	}
	else
	{
		_FileStream()._fread(&rBoolData, sizeof(bool), 1);
	}
}

void CFileSerializer::Serialize(char & rCharData)
{
	if(m_bWriteMode)
	{
		_FileStream()._fwrite(&rCharData, sizeof(char), 1);
	}
	else
	{
		_FileStream()._fread(&rCharData, sizeof(char), 1);
	}
}
void CFileSerializer::Serialize(unsigned char & rUCharData)
{
	if(m_bWriteMode)
	{
		_FileStream()._fwrite(&rUCharData, sizeof(unsigned char), 1);
	}
	else
	{
		_FileStream()._fread(&rUCharData, sizeof(unsigned char), 1);
	}
}
void CFileSerializer::Serialize(short & rShortData)
{
	if(m_bWriteMode)
	{
		_FileStream()._fwrite(&rShortData, sizeof(short), 1);
	}
	else
	{
		_FileStream()._fread(&rShortData, sizeof(short), 1);
	}
}
void CFileSerializer::Serialize(unsigned short & rUShortData)
{
	if(m_bWriteMode)
	{
		_FileStream()._fwrite(&rUShortData, sizeof(unsigned short), 1);
	}
	else
	{
		_FileStream()._fread(&rUShortData, sizeof(unsigned short), 1);
	}
}
void CFileSerializer::Serialize(int & rIntData)
{
	if(m_bWriteMode)
	{
		_FileStream()._fwrite(&rIntData, sizeof(int), 1);
	}
	else
	{
		_FileStream()._fread(&rIntData, sizeof(int), 1);
	}
}
void CFileSerializer::Serialize(unsigned int & rUIntData)
{
	if(m_bWriteMode)
	{
		_FileStream()._fwrite(&rUIntData, sizeof(unsigned int), 1);
	}
	else
	{
		_FileStream()._fread(&rUIntData, sizeof(unsigned int), 1);
	}
}
void CFileSerializer::Serialize(__int64 & rInt64Data)
{
	if(m_bWriteMode)
	{
		_FileStream()._fwrite(&rInt64Data, sizeof(__int64), 1);
	}
	else
	{
		_FileStream()._fread(&rInt64Data, sizeof(__int64), 1);
	}
}
void CFileSerializer::Serialize(unsigned __int64 & rUInt64Data)
{
	if(m_bWriteMode)
	{
		_FileStream()._fwrite(&rUInt64Data, sizeof(unsigned __int64), 1);
	}
	else
	{
		_FileStream()._fread(&rUInt64Data, sizeof(unsigned __int64), 1);
	}
}
void CFileSerializer::Serialize(float & rFloatData)
{
	if(m_bWriteMode)
	{
		_FileStream()._fwrite(&rFloatData, sizeof(float), 1);
	}
	else
	{
		_FileStream()._fread(&rFloatData, sizeof(float), 1);
	}
}
void CFileSerializer::Serialize(double & rDoubleData)
{
	if(m_bWriteMode)
	{
		_FileStream()._fwrite(&rDoubleData, sizeof(double), 1);
	}
	else
	{
		_FileStream()._fread(&rDoubleData, sizeof(double), 1);
	}
}

void CFileSerializer::Serialize(std::string & rStrData)
{
	if(m_bWriteMode)
	{
		int nSize = (int)rStrData.size();
		Serialize(nSize);
		if(nSize)
		{
			_FileStream()._fwrite(rStrData.c_str(), sizeof(char), nSize);
		}
	}
	else
	{
		int nSize(0);
		Serialize(nSize);
		if(nSize)
		{
			rStrData.resize( nSize );
			_FileStream()._fread((char*)rStrData.c_str(), sizeof(char), nSize);
		}
	}
}
void CFileSerializer::Serialize(char * pData, int nSize)
{
	if(m_bWriteMode)
	{
		_FileStream()._fwrite(pData, sizeof(char), nSize);
	}
	else
	{
		_FileStream()._fread(pData, sizeof(char), nSize);
	}
}